﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chefbook.DataObjects;
using System.Globalization;
using Chefbook.ViewModels;

namespace Chefbook.UserActions
{
    /// <summary>
    /// Action for deleting a recipe
    /// </summary>
    public class DeleteRecipeAction : UserActionBase
    {
        private RecipeViewModel recipeToDelete;
        private List<CategoryViewModel> categories;

        /// <summary>
        /// Creates a new action to delete a recipe
        /// </summary>
        /// <param name="recipeToDelete"></param>
        public DeleteRecipeAction(RecipeViewModel recipeToDelete)
        {
            this.recipeToDelete = recipeToDelete;
            this.categories = new List<CategoryViewModel>(recipeToDelete.Categories);
        }

        /// <summary>
        /// Deletes the recipe, removing all references to it
        /// </summary>
        public override void DoAction()
        {
            base.DoAction();
            recipeToDelete.RecipeModel.Delete();
        }

        /// <summary>
        /// Restores the recipe
        /// </summary>
        public override void UndoAction()
        {
            base.UndoAction();
            categories.ForEach(category => category.AddRecipe(recipeToDelete));
        }

        /// <summary>
        /// Returns a user-friendly string describing the action
        /// </summary>
        /// <returns>A user-friendly string describing the action</returns>
        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentUICulture, "Delete Recipe '{0}'", recipeToDelete);
        }
    }
}
